package cn.csust.net2.manager.client.ux;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.csust.net2.manager.shared.po.Comprehensive;
import cn.csust.net2.manager.shared.po.Exam;
import cn.csust.net2.manager.shared.po.HeldPosition;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.PartyTraining;
import cn.csust.net2.manager.shared.po.Punishment;
import cn.csust.net2.manager.shared.po.RatifyJoinParty;
import cn.csust.net2.manager.shared.po.Resume;
import cn.csust.net2.manager.shared.po.Reward;
import cn.csust.net2.manager.shared.util.Factory;
import cn.csust.net2.manager.shared.util.Utils;
import cn.csust.net2.manager.shared.xml.PropertyAssist;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;

public abstract class QueryWindow extends Window{
	public QueryWindow(){
		this.setSize(400, 500);
		this.setPlain(true);
		this.setModal(true);
		this.setBlinkModal(true);
		this.setHeading("高级查询");
		this.setLayout(new FitLayout());
		setScrollMode(Scroll.AUTO);
		init();
		
		
		 
	}
	Map<String,Map<String,String>> map = new HashMap<String,Map<String,String>>();
	FormPanel form2 = new FormPanel();  
	public void init(){
		form2 = new FormPanel();  
		form2.setFrame(true);  
		form2.setScrollMode(Scroll.AUTO);
		form2.setHeading("高级查询");  
		form2.setWidth(350);  
		form2.setLayout(new FlowLayout());  
		
		form2.add(this.readXml(new PartyTraining()));  
		form2.add(this.readXml(new Punishment()));  
		form2.add(this.readXml(new Resume()));  
		form2.add(this.readXml(new Reward()));  
		form2.add(this.readXml(new HeldPosition())); 
		form2.add(this.readXml(new Exam())); 
		form2.add(this.readXml(new Comprehensive())); 
		form2.add(this.readXml(new RatifyJoinParty())); 
		
		form2.setButtonAlign(HorizontalAlignment.CENTER);  
		Button save = new Button("保存");
		Button canel = new Button("放弃");
		save.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@SuppressWarnings({ "unchecked", "deprecation" })
			@Override
			public void componentSelected(ButtonEvent ce) {
				List<Field<?>> fields = form2.getFields();
				for(Field<?> f :fields){
					Object value = null;
					if(f instanceof DateField){
						value = ((DateField)f).getValue();
						if(value!=null&&!value.equals(""))
							value = Utils.dateFormat((Date)value);
					}else if(f instanceof NumberField){
						value = ((NumberField)f).getValue();
					}else if(f instanceof SimpleComboBox){
						if( ((SimpleComboBox<String>)f).getValue()!=null){
							value = ((SimpleComboBox<String>)f).getValue().getValue();
							
						}
					}else if(f instanceof TextField){
						value = f.getValue();
					}
					String name  = f.getData("name");
					String class_name  = f.getData("class_name");
					System.out.println("class_name为"+class_name+"name为"+name+"value为"+value);
					if(value!=null&&!value.equals("")&&!value.equals("null")){
						Map<String,String> maps = map.get(class_name);
						if(maps==null){
							maps = new HashMap<String,String>();
							map.put(class_name, maps);
						}
						maps.put(name, value.toString());
					}
				}
				serverCommit(map);
				QueryWindow.this.close();
				Info.display("系统信息", "保存");
			}
		});
		canel.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				QueryWindow.this.close();
			}
		});
		form2.addButton(save);
		form2.addButton(canel);
		this.add(form2);
		show();
	}
	/**
	 * 使用这个高级查询窗口的提交方法
	 * @param map
	 */
	public abstract void serverCommit(Map<String,Map<String,String>> map);
	public FieldSet readXml(PO po){
		List<PropertyAssist> list = po.getAssist().getPropertyAssists();
		FieldSet fieldSet = new FieldSet();  
		
		fieldSet.setHeading(po.getDisplayName());  
	    fieldSet.setCollapsible(true); 
	    FormLayout   layout = new FormLayout();  
		layout.setLabelWidth(75);  
		FormData formData = new FormData("-20"); 
		fieldSet.setLayout(layout); 
		for(PropertyAssist propertyAssist:list){
			EditorType et = propertyAssist.getEditorType();
			if(et==null)
				et = EditorType.TextField;
			switch(et){
				
			case DateField:
				DateField dateField = Factory.createDateFieldWithFormated();
				dateField.setAllowBlank(true);
				dateField.setData("class_name", po.getAssist().getClassName());
				dateField.setFieldLabel(propertyAssist.getDisplayName()+"起始日期");
				dateField.setData("name", propertyAssist.getName());
				fieldSet.add(dateField, formData);
				break;
			case NumberField_Double:
				NumberField field1 = new NumberField();
				field1.setData("class_name", po.getAssist().getClassName());
				field1.setAllowBlank(true);
				field1.setPropertyEditorType(Double.class);
			    field1.setFieldLabel(propertyAssist.getDisplayName());  
			    field1.setData("name", propertyAssist.getName());
			    fieldSet.add(field1, formData);  
				break;
			case NumberField_Integer:
				NumberField field2 = new NumberField();
				field2.setAllowBlank(true);
				field2.setData("class_name", po.getAssist().getClassName());
				field2.setPropertyEditorType(Integer.class);
			    field2.setFieldLabel(propertyAssist.getDisplayName());  
			    field2.setData("name", propertyAssist.getName());
			    fieldSet.add(field2, formData);  
				break;
			case NumberField_Long:
				NumberField field3 = new NumberField();
				field3.setAllowBlank(true);
				field3.setPropertyEditorType(Long.class);
				field3.setData("class_name", po.getAssist().getClassName());
				field3.setData("name", propertyAssist.getName());
			    field3.setFieldLabel(propertyAssist.getDisplayName());  
			    fieldSet.add(field3, formData);  
				break;
			case SimpleBox:
				SimpleComboBox<String> combo = Utils.createSimpleComboBox(propertyAssist.getSimpleBox());
				combo.setFieldLabel(propertyAssist.getDisplayName());
				combo.setData("class_name", po.getAssist().getClassName());
				combo.setData("name", propertyAssist.getName());
				fieldSet.add(combo, formData);  
				break;
			case TextField:
				TextField<String> field4 = new TextField<String>();  
			    field4.setFieldLabel(propertyAssist.getDisplayName()); 
			    field4.setData("class_name", po.getAssist().getClassName());
			    field4.setData("name", propertyAssist.getName());
			    fieldSet.add(field4, formData);  
				break;
			
			}
		}
		return fieldSet;
	}
	
}
